package leetcode;

import java.util.HashSet;

/**
 * @创建人 mby381
 * @创建时间 2022/3/12
 * @描述
 */
public class Solution1790 {
    public boolean areAlmostEqual(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        if (s1.length() != s2.length()) {
            return false;
        }
        int len = s1.length();
        int tag = 0;
        int[] nonnum = new int[4];
        //相当于缓冲池
        for (int i = 0; i < len; i++) {
            if (s1.charAt(i) != s2.charAt(i)) {
                if (tag == 2) {
                    return false;
                }
                nonnum[tag * 2] = s1.charAt(i);
                nonnum[tag * 2 + 1] = s2.charAt(i);
                tag++;
            }
        }
        if (tag == 1) {
            return false;
        }
        //只有一位不同
        return nonnum[0] == nonnum[3] && nonnum[1] == nonnum[2];
    }

    public boolean areAlmostEqualFalse(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        int len = s1.length();
        HashSet<Character> h1 = new HashSet<>();
        HashSet<Character> h2 = new HashSet<>();
        for (int i = 0; i < len; i++) {
            h1.add(s1.charAt(i));
            h2.add(s1.charAt(i));
        }
        if (!h1.equals(h2)) {
            return false;
        }
        return true;
    }
}